/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "WZImage.h"

#include "WZSubProperty.h"

using namespace std;

namespace WZLib{
	int&				WZImage::blockSize(){return this->_blockSize;}
	int&				WZImage::checksum(){return this->_checksum;}
	unsigned int&		WZImage::offset(){return this->_offset;}
	WZSubProperty*		WZImage::sub(){return this->_sub;}
	WZBinaryReader**	WZImage::reader(){return &(this->_reader);}
	ctor WZImage::WZImage(string name):WZObject(name,WZObject::Image){
		this->_sub=NULL;
		this->_blockSize=this->_checksum=0;
		this->_offset=0;
		this->_reader=NULL;
	}
	dtor WZImage::~WZImage(){
		delete this->_sub;
	}
	ErrorCode WZImage::parse(WZLib::WZBinaryReader *reader){
		if(this->_parsed)
			return Error_WZLib_WZImage_Parse_AlreadyParsed;
		reader=this->_reader;
		long originalPos=reader->Position();
		reader->Position(this->_offset);
		byte b=reader->ReadByte();
		if(b!=0x73 || reader->ReadEncryptedString()!="Property" || reader->ReadUInt16()!=0)
			return Error_WZLib_WZImage_Parse_Error_1;
		this->_sub=new WZSubProperty(this,this->_name+" root sub");
		this->_parsed=true;
		return this->_sub->parse(reader,this->_offset);
	}
	ErrorCode WZImage::unparse(){
		if(!this->_parsed)
			return Error_NoError;
		delete this->_sub;
		return Error_NoError;
		this->_parsed=false;
	}
	void WZImage::resolveUOLs(){
		this->_sub->resolveUOLs();
	}
};